<template>
	<div class="todo-container">
		<div class="todo-wrap">
			<Header :addTodo="addTodo"/>
			<List :todos="todos" :deleteTodo="deleteTodo" :checkTodo="checkTodo"/>
			<Footer :clearAllDone="clearAllDone" :todos="todos" :checkAll="checkAll"/>
		</div>
	</div>
</template>

<script>
	import Header from './components/Header'
	import List from './components/List'
	import Footer from './components/Footer'

	export default {
		name: 'App',
		components:{Header,List,Footer},
		data(){
			return{
				todos:[
					{id:'001',title:'吃饭',done:true},
					{id:'002',title:'睡觉',done:false},
					{id:'003',title:'打台球',done:true},
					{id:'004',title:'打代码',done:false}
				]
			}
		},
		methods: {
			//添加一个todo
			addTodo(todoObj){
				this.todos.unshift(todoObj)
			},
			//删除一个todo
			deleteTodo(id){
				this.todos = this.todos.filter(todoObj => todoObj.id !== id)
			},
			//勾选 or 取消勾选
			checkTodo(id,done){
				const targetTodo = this.todos.find((todoObj)=>{
					// console.log('@')
					return todoObj.id === id
				})
				targetTodo.done = done
			},
			//清除所有已完成
			clearAllDone(){
				this.todos = this.todos.filter(todoObj => !todoObj.done )

				//以下代码是违反原则的，不要在数组的遍历方法中，引起数组长度的变化
				/* this.todos.forEach((todoObj,index)=>{
					if(todoObj.done){
						this.todos.splice(index,1)
					}
				}) */
			},
			//全选 or 取消全选
			checkAll(done){
				this.todos.forEach((todo)=>{
					todo.done = done
				})
			}
		},
	}
</script>
